package com.kehutong.crm.message.micro;

import java.sql.Timestamp;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import com.kehutong.common.TokenService;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.crm.message.service.MessageAccountConfigService;
import com.kehutong.crm.message.service.MessageService;

@MvcAction
@ReqMapping("/admin/micro/crm/message")
public class MessageAction {

    @Inject
    private MessageService messageService;
    @Inject
    private TokenService tokenService;
    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private MessageAccountConfigService configService;

    @ReqMapping("/bulletin/list")
    public Object bulletinList(Token token, JSONObject jsonObject){

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        return Result.success(messageService.bulletinList(jsonObject, token.getUuid(), token.getCompanyNo(), new Timestamp(token.getUserCreateTime()), false));
    }

    @ReqMapping("/bulletin/detail")
    public Object bulletinDetail(String id){
        return messageService.bulletinDetail(id);
    }

    @ReqMapping("/all/list")
    public Object allList(Token token, JSONObject jsonObject){

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        return Result.success(messageService.allList(jsonObject, token.getUuid(), token.getCompanyNo(), new Timestamp(token.getUserCreateTime()), false));
    }

    @ReqMapping("/business/read")
    public Object businessRead(String id){
        messageService.businessRead(id);
        return Result.success();
    }

    @ReqMapping("/business/read/all")
    public Object businessRead(Token token){
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        messageService.businessReadAll(token.getUuid());
        return Result.success();
    }

    @ReqMapping("/account/config")
    public Object accountConfig(Token token){
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        return Result.success(configService.getAccountConfig(token.getUuid()));
    }

    @ReqMapping("/update/account/config")
    public Object updateAccountConfig(JSONObject jsonObject){
        return configService.updateAccountConfig(jsonObject);
    }

    private boolean checkLogin(Token token) {
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) || Objects.isEmpty(token.getUsername()) || token.getUserCreateTime() == 0L) {
            return false;
        } else {
            return true;
        }
    }

}
